package org.fqh.system.controller;

import java.util.ArrayList;
import java.util.List;

import org.fqh.common.base.Constants;
import org.fqh.system.entity.TSysResource;
import org.fqh.system.entity.TreeVo;
import org.fqh.system.service.TSysResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
* @author fqh 
* @version 创建时间：2018年1月16日 下午3:17:52
* 类说明 : 资源页面 
*/
@Controller
@RequestMapping("/resource")
public class TSysResourceController {
	
	@Autowired 
	TSysResourceService resourceService;
	
	@RequestMapping("/list")
	public String list(Model model,@RequestParam(required=false,defaultValue="1")int pageNO) {
	    int size=Constants.PAGE_SIZE; 
		PageHelper.startPage(pageNO, size);
	    List<TSysResource> resource =new ArrayList<>();
		try {
			resource = resourceService.selectAll();
		    model.addAttribute("list", resource);
		    PageInfo<TSysResource> page = new PageInfo<TSysResource>(resource);
		    model.addAttribute("page", page);
		    return "system/resource/resourceList";
		} catch (Exception e) {
			e.printStackTrace();
			return "500";
		}
	}
	@RequestMapping("/add")
	public String add(Model model,String pid,String sort) {
    	model.addAttribute("pid",pid);
    	model.addAttribute("sort",sort);	
		return "system/resource/resourceAdd";
	}
    @RequestMapping("/save")
    @ResponseBody
	public String save(Model model,TSysResource resource) {
    	String result = new String();
		try {
			resourceService.insert(resource);
			result = "success";
		}catch(Exception e){
			e.printStackTrace();
			result = "fail";
		}
 
	    return result;
	}
    @RequestMapping("/update")
    @ResponseBody
	public String update(Model model,TSysResource resource) {
    	String result = new String();
		try {
			resourceService.update(resource);
			result = "success";
		}catch(Exception e){
			e.printStackTrace();
			result = "fail";
		}
 
	    return result;
	}
 	@RequestMapping("/delete")
    @ResponseBody
    public String delete(String id) {
    	String result = new String();
    	try {
    	    resourceService.delete(Integer.parseInt(id));
    		result = "success";
    	}catch(Exception e) {
    		e.printStackTrace();
    		result = "fail";
    	}
    	
		return result;	
    }
 	@RequestMapping("/tree")
 	public String resourceTree() {
 		return "system/resource/resourceTree";
 	}
 	
 	@ResponseBody
 	@RequestMapping(value="/treeList")
 	public String treeList() {
	    List<TSysResource> resource =new ArrayList<>();
	    String jsonStr = new String();
		try {
			resource = resourceService.selectAll();
			List<TreeVo> treeList = new ArrayList<TreeVo>();
			for(TSysResource temp:resource) {
		       TreeVo tree = new TreeVo();
		       tree.setId(temp.getId());
		       tree.setPid(temp.getPid());
		       tree.setName(temp.getName());
		       treeList.add(tree);
			}
			jsonStr=JSON.toJSONString(treeList);
			System.out.println(jsonStr);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonStr;
 	}
	@ResponseBody
 	@RequestMapping(value="/treeListIndex")
 	public String treeListIndex() {
	    List<TSysResource> resource =new ArrayList<>();
	    String jsonStr = new String();
		try {
			resource = resourceService.selectAll();
			List<TreeVo> treeList = new ArrayList<TreeVo>();
			for(TSysResource temp:resource) {
		       TreeVo tree = new TreeVo();
		       tree.setId(temp.getId());
		       tree.setPid(temp.getPid());
		       tree.setName(temp.getName());
		       tree.setUrl(temp.getUrl());
		       treeList.add(tree);
			}
			jsonStr=JSON.toJSONString(treeList);
			System.out.println(jsonStr);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonStr;
 	}
 	@RequestMapping("/getResource")
 	public String getResourceById(Model model,int id) {
		 
		try {
			TSysResource entity  = resourceService.GetById(id);
			model.addAttribute("entity",entity);
		} catch (Exception e) {
			e.printStackTrace();
		}
 		return "/system/resource/resourceDetail";
 	}
}
